package com.redwall.threads;

public class ThreadLoco {
    private static ThreadLocal<String> threadLocal = new ThreadLocal<>();

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            threadLocal.set("thread   --  1");
            //调用打印方法
            printAndRemove(Thread.currentThread().getName());
            System.out.println("after remove : " + threadLocal.get());
        });


        Thread t2 = new Thread(() -> {
            threadLocal.set("thread   --  2");
            //调用打印方法
            printAndRemove(Thread.currentThread().getName());
            System.out.println("after remove : " + threadLocal.get());
        });

        t1.start();
        t2.start();
    }

    private static void printAndRemove(String name) {
        //打印当前线程中本地内存中本地变量的值
        System.out.println(name + " :" + threadLocal.get());
        //清除本地内存中的本地变量
        threadLocal.remove();
    }


}
